package com.ruoyi.mall.product.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.to.SkuReductionTo;
import com.ruoyi.common.core.to.SpuBoundTo;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.mall.product.domain.entity.*;
import com.ruoyi.mall.product.domain.repository.*;
import com.ruoyi.mall.product.domain.service.IPmsSkuAttrValueService;
import com.ruoyi.mall.product.domain.service.IPmsSkuImagesService;
import com.ruoyi.mall.product.domain.service.IPmsSpuAttrValueService;
import com.ruoyi.mall.product.domain.service.IPmsSpuService;
import com.ruoyi.mall.product.domain.vo.*;
//import com.ruoyi.product.api.RemoteCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * spu信息Service业务层处理
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@Service
@Slf4j
public class PmsSpuServiceImpl implements IPmsSpuService {
    @Autowired
    private PmsSpuMapper pmsSpuMapper;

    @Autowired
    private PmsSpuDescMapper pmsSpuDescMapper;

    @Autowired
    private PmsSpuImagesMapper pmsSpuImagesMapper;

    @Autowired
    private PmsAttrMapper pmsAttrMapper;

    @Autowired
    private PmsSpuAttrValueMapper pmsSpuAttrValueMapper;

    @Autowired
    private IPmsSpuAttrValueService pmsSpuAttrValueService;

    @Autowired
    private PmsSkuMapper pmsSkuMapper;

    @Autowired
    private IPmsSkuImagesService pmsSkuImagesService;

    @Autowired
    private IPmsSkuAttrValueService pmsSkuAttrValueService;

//    @Autowired
//    private RemoteCouponService remoteCouponService;


    /**
     * 查询spu信息
     *
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public PmsSpu selectPmsSpuById(Long id) {
        return pmsSpuMapper.selectById(id);
    }

    /**
     * 查询spu信息列表
     *
     * @param pmsSpu spu信息
     * @return spu信息
     */
    @Override
    public List<PmsSpu> selectPmsSpuList(PmsSpu pmsSpu) {
        QueryWrapper<PmsSpu> wrapper = new QueryWrapper<>();

        String key = pmsSpu.getKey();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(i -> {
                i.eq("id", key).or().like("spu_name", key);
            });

        }
        Integer publishStatus = pmsSpu.getPublishStatus();
        if (!StringUtils.isNull(publishStatus)) {
            wrapper.eq("publish_status", publishStatus);
        }

        Long brandId = pmsSpu.getBrandId();
        if (!StringUtils.isNull(brandId)) {
            wrapper.eq("brand_id", brandId);
        }
        Long categoryId = pmsSpu.getCategoryId();
        if (!StringUtils.isNull(categoryId)) {
            wrapper.eq("category_id", categoryId);
        }
        return pmsSpuMapper.selectList(wrapper);
    }

    /**
     * 新增spu信息
     *
     * @param pmsSpu spu信息
     * @return 结果
     */
    @Override
    public int insertPmsSpu(PmsSpu pmsSpu) {
        pmsSpu.setCreateTime(DateUtils.getNowDate());
        return pmsSpuMapper.insert(pmsSpu);
    }

    /**
     * 修改spu信息
     *
     * @param pmsSpu spu信息
     * @return 结果
     */
    @Override
    public int updatePmsSpu(PmsSpu pmsSpu) {
        pmsSpu.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuMapper.updateById(pmsSpu);
    }

    /**
     * 批量删除spu信息
     *
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuByIds(Long[] ids) {
        return pmsSpuMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除spu信息信息
     *
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuById(Long id) {
        return pmsSpuMapper.deleteById(id);
    }

    /**
     * 保存商品
     *
     * @param spuSaveVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int savePmsSpuInfo(SpuSaveVo spuSaveVo) {
//        //1.保存spu基本信息 pms_spu
//        PmsSpu pmsSpu = new PmsSpu();
//        BeanUtils.copyProperties(spuSaveVo, pmsSpu);
//        pmsSpu.setCreateTime(new Date());
//        pmsSpu.setUpdateTime(new Date());
//        pmsSpuMapper.insert(pmsSpu);
//
//
//        //2.保存spu的描述图片 pms_spu_desc
//        List<String> decript = spuSaveVo.getDecript();
//        PmsSpuDesc pmsSpuDesc = new PmsSpuDesc();
//        pmsSpuDesc.setSpuId(pmsSpu.getId());
//        pmsSpuDesc.setDecript(String.join(",", decript));
//        pmsSpuDescMapper.insertPmsSpuDesc(pmsSpuDesc);
//
//        //3.保存spu的图片集   pms_spu_images
//        List<String> images = spuSaveVo.getImages();
//        if (!StringUtils.isEmpty(images)) {
//            images.forEach(image -> {
//                PmsSpuImages pmsSpuImages = new PmsSpuImages();
//                pmsSpuImages.setSpuId(pmsSpu.getId());
//                pmsSpuImages.setImgUrl(image);
//                pmsSpuImagesMapper.insert(pmsSpuImages);
//            });
//        }
//
//        //4.保存spu的规格参数  pms_spu_attr_value
//        List<BaseAttrs> baseAttrs = spuSaveVo.getBaseAttrs();
//        List<PmsSpuAttrValue> attrValueList = baseAttrs.stream().map(attr -> {
//            PmsSpuAttrValue pmsSpuAttrValue = new PmsSpuAttrValue();
//            pmsSpuAttrValue.setAttrId(attr.getId());
//            PmsAttr pmsAttr = pmsAttrMapper.selectPmsAttrById(attr.getId());
//            pmsSpuAttrValue.setAttrName(pmsAttr.getName());
//            pmsSpuAttrValue.setAttrValue(attr.getValueSelect());
//            pmsSpuAttrValue.setSpuId(pmsSpu.getId());
//            pmsSpuAttrValue.setSort(attr.getShowDesc());
//            return pmsSpuAttrValue;
//        }).collect(Collectors.toList());
//        boolean b = pmsSpuAttrValueService.saveBatch(attrValueList);
//
//
//        //TODO 远程保存 保存积分信息  sms_spu_bounds
//        Bounds bounds = spuSaveVo.getBounds();
//        SpuBoundTo spuBoundTo = new SpuBoundTo();
//        BeanUtils.copyProperties(bounds, spuBoundTo);
//        spuBoundTo.setSpuId(pmsSpu.getId());
//        R r = remoteCouponService.saveSpuBounds(spuBoundTo);
//        if (r.getCode() != 200) {
//            log.error("积分信息远程保存失败");
//        }
//
//
//        //5.保存当前spu对应的所有sku信息
//
//        List<Skus> skus = spuSaveVo.getSkus();
//        if (!StringUtils.isEmpty(skus)) {
//            skus.forEach(sku -> {
//                String defaultImg = "";
//                for (Images image : sku.getImages()) {
//                    //如果是1就代表是默认图片
//                    if (image.getDefaultImg() == 1) {
//                        defaultImg = image.getImgUrl();
//                    }
//                }
//                PmsSku pmsSku = new PmsSku();
//                BeanUtils.copyProperties(sku, pmsSku);
//                pmsSku.setBrandId(pmsSpu.getBrandId());
//                pmsSku.setCategoryId(pmsSpu.getCategoryId());
//                pmsSku.setSaleCount(0L);
//                pmsSku.setSpuId(pmsSpu.getId());
//                pmsSku.setSkuDefaultImage(defaultImg);
//
//                //5.1  sku的基本信息  pms_sku
//                pmsSkuMapper.insert(pmsSku);
//
//                Long pmsSkuId = pmsSku.getId();
//                List<PmsSkuImages> pmsSkuImagesList = sku.getImages().stream().map(img -> {
//                    PmsSkuImages pmsSkuImages = new PmsSkuImages();
//                    pmsSkuImages.setSkuId(pmsSkuId);
//                    pmsSkuImages.setUrl(img.getImgUrl());
//                    pmsSkuImages.setDefaultStatus(img.getDefaultImg());
//                    return pmsSkuImages;
//                }).filter(x -> !StringUtils.isEmpty(x.getUrl())).collect(Collectors.toList());
//
//                //5.2  sku的图片信息  pms_sku_images
//                //TODO 没有图片路径的无需保存
//                pmsSkuImagesService.saveBatch(pmsSkuImagesList);
//
//                List<Attr> attr = sku.getAttr();
//                List<PmsSkuAttrValue> pmsSkuAttrValueList = attr.stream().map(a -> {
//                    PmsSkuAttrValue pmsSkuAttrValue = new PmsSkuAttrValue();
//                    BeanUtils.copyProperties(a, pmsSkuAttrValue);
//                    pmsSkuAttrValue.setSkuId(pmsSkuId);
//
//                    return pmsSkuAttrValue;
//                }).collect(Collectors.toList());
//                //5.3  sku的销售属性信息 pms_sku_attr_value
//                pmsSkuAttrValueService.saveBatch(pmsSkuAttrValueList);
//
//                //5.4 TODO sku的优惠，满减等信息  sms库  sms_sku_ladder  sms_sku_full_reduction sms_member_price
//                SkuReductionTo skuReductionTo = new SkuReductionTo();
//                BeanUtils.copyProperties(sku, skuReductionTo);
//                skuReductionTo.setSkuId(pmsSkuId);
//                if (skuReductionTo.getFullCount() > 0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
//                    R r1 = remoteCouponService.saveSkuReduction(skuReductionTo);
//                    if (r1.getCode() != 200) {
//                        System.out.println("远程保存sku优惠信息失败");
//                    }
//                }
//            });
//        }

//        return b ? 1 : 0;
        return 0;
    }
}
